package com.woniu.his.app.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.app.client.AppRegistrationClient;
import com.woniu.his.app.client.param.PhoneLoginParam;
import com.woniu.his.app.client.param.RegistrationParam;
import com.woniu.his.app.client.result.DeptAllDoctorResult;
import com.woniu.his.app.client.result.DeptAllResult;
import com.woniu.his.app.client.result.RegHistoryResult;
import com.woniu.his.app.client.result.StaffSkdInfoResult;
import com.woniu.his.app.common.util.JwtUtil;
import com.woniu.his.app.service.service.RegistrationService;
import com.woniu.his.app.service.service.dto.DeptAllDoctorDto;
import com.woniu.his.app.service.service.dto.DeptAllDto;
import com.woniu.his.app.service.service.dto.RegHistoryDto;
import com.woniu.his.app.service.service.dto.StaffSkdInfoDto;
import com.woniu.his.app.service.service.param.PhoneLoginServiceParam;
import com.woniu.his.app.service.service.param.RegistrationServiceParam;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/app")
public class AppRegistrationApiImpl implements AppRegistrationClient {

    @Resource
    private RegistrationService registrationService;

    @Override
    public Result registration(@Valid RegistrationParam param) {

        log.info("挂号上传的数据：{}",param);
        RegistrationServiceParam registration= BeanUtil.copyProperties(param,RegistrationServiceParam.class);
        registrationService.registration(registration);
        return Result.getInstance();
    }

    @Override
    public Result<List<DeptAllResult>> findAllDept() {

        log.info("查询所有科室");
        List<DeptAllDto> list=registrationService.findAllDept();
        List<DeptAllResult> results=BeanUtil.copyToList(list,DeptAllResult.class);
        return Result.getInstanceList(DeptAllResult.class).setData(results);
    }

    @Override
    public Result<List<DeptAllDoctorResult>> findAllDoctorByDeptId(Integer deptId) {

        log.info("传回来的科室ID:{}",deptId);
        List<DeptAllDoctorDto> list=registrationService.findAllDoctorByDeptId(deptId);
        List<DeptAllDoctorResult> results=BeanUtil.copyToList(list,DeptAllDoctorResult.class);
        return Result.getInstanceList(DeptAllDoctorResult.class).setData(results);
    }

    @Override
    public Result<String> phoneLogin(@Valid PhoneLoginParam param) {

        log.info("登陆传回来的信息：{}",param);
        PhoneLoginServiceParam loginServiceParam=BeanUtil.copyProperties(param,PhoneLoginServiceParam.class);
        Map map=registrationService.phoneLogin(loginServiceParam);
        String token = JwtUtil.create(map);
        log.info("登录成功的token:{}",token);
        return Result.getInstance(String.class).setData(token);
    }

    @Override
    public Result getCode(String userPhone) {

        log.info("手机号为：{}",userPhone);
        registrationService.getCode(userPhone);
        return Result.getInstance();
    }

    @Override
    public Result<List<StaffSkdInfoResult>> queryStaffAllSkdByStaffId(Integer staffId) {

        log.info("传回来的员工ID:{}",staffId);
        List<StaffSkdInfoDto> list=registrationService.queryStaffAllSkdByStaffId(staffId);
        List<StaffSkdInfoResult> results=BeanUtil.copyToList(list,StaffSkdInfoResult.class);
        return Result.getInstanceList(StaffSkdInfoResult.class).setData(results);
    }

    @Override
    public Result<RegHistoryResult> queryRegByBillId(Long billId) {
        log.info("根据账单id查询挂号数据：{}",billId);
        RegHistoryDto regHistoryDto = registrationService.queryRegByBillId(billId);
        RegHistoryResult regHistoryResult = BeanUtil.copyProperties(regHistoryDto, RegHistoryResult.class);
        return Result.getInstance(RegHistoryResult.class).setData(regHistoryResult);
    }

    @Override
    public Result<Boolean> paySuccess(Long billId) {
        log.info("支付成功修改状态：{}",billId);
        Boolean bool = registrationService.paySuccess(billId);
        return Result.getInstance(Boolean.class).setData(bool);
    }

}
